package com.lysj.bhp.user.controller;

import com.lysj.bhp.investment.domain.Reward;
import com.lysj.bhp.user.base.UserContent;
import com.lysj.bhp.user.domain.BatchPlayCoin;
import com.lysj.bhp.user.domain.BatchPlayCoinRecord;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.user.service.BatchPlayCoinService;
import com.lysj.bhp.user.util.MyWallet;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author zk
 * @description
 * @create 2018-07-31 10:11
 * 批量打币（未存入数据库）
 * 在serveice层创建一个静态的List，用来存导入的数据，最后用来显示在界面上
 **/
@RestController
@RequestMapping("/admin/batch_play_coin")
public class BatchPlayCoinController extends UserContent {

    Logger logger = LoggerFactory.getLogger(BatchPlayCoinController.class);

    @Resource
    private MyWallet myWallet;

    @GetMapping("list")
    public Resp list() {
        return new Resp(batchPlayCoinService.batchPlayCoinList);
    }

    //批量打币功能
    @PostMapping("batch")
    public Resp batch(String ids) throws Exception {
        logger.info("批量打币ID {}", ids.toString());
        //获取前台选中的ID
        String[] id = ids.split(",");
        if (id.length == 0) {
            return new Resp(Resp.PARAM_ERROR, "请选择");
        }
        for (String batchid : id) {
            int i = Integer.parseInt(batchid);
            //根据选中的ID从service层静态的List集合中查找所对应的数据
            BatchPlayCoin batchPlayCoin = batchPlayCoinService.batchPlayCoinList.get(i - 1);
            //根据邮箱从数据库查找对应的用户
            User user = userRepository.findByEmail(batchPlayCoin.getUserEmail());
            //如果查找为空，说明邮箱不存在，就修改打币状态
            if (user == null) {
                batchPlayCoin.setCoinStatus("邮箱不存在");
            } else {
                //已打过币的要跳过
                if (!batchPlayCoin.getCoinStatus().equals("已打币")) {
                    //判断币种 bhpc btc eth 处理所对应的币种
                    if (batchPlayCoin.getCoinType().equalsIgnoreCase("bhpc")) {
                        //根据用户查找该币种的钱包
                        BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(user.getUserId());
                        //首先判断钱包不存在的情况，否则就是钱包存在
                        if (null == bhpcWallet) {
                            //选择减少操作,则提示用户余额为0;选择增加操作,给用户创建钱包,同时更新钱包的总额跟可用余额
                            if (batchPlayCoin.getOperation().equals("减少")) {
                                batchPlayCoin.setCoinStatus(" 数量不足");
                            } else {
                                //这边是钱包打币增加的情况
                                bhpcWallet = myWallet.createBhpcWallet(user.getUserId());
                                //钱包可用余额增加
                                bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().add(batchPlayCoin.getCoinSum()));
                                //钱包余额增加
                                bhpcWallet.setBalance(bhpcWallet.getBalance().add(batchPlayCoin.getCoinSum()));
                                //设置打币状态
                                batchPlayCoin.setCoinStatus("已打币");
                                //保存
                                batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                bhpcWalletRepository.save(bhpcWallet);
                            }
                        } else {
                            //当选择减少币种该操作,判定用户的钱包数量是否足够
                            if (batchPlayCoin.getOperation().equals("减少")) {
                                //数量不足,返回提示;数量足够更新钱包余额
                                if (bhpcWallet.getAvailBalance().compareTo(batchPlayCoin.getCoinSum()) == -1) {
                                    batchPlayCoin.setCoinStatus("数量不足");
                                } else {
                                    bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().subtract(batchPlayCoin.getCoinSum()));
                                    bhpcWallet.setBalance(bhpcWallet.getBalance().subtract(batchPlayCoin.getCoinSum()));
                                    batchPlayCoin.setCoinStatus("已打币");
                                    batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                    bhpcWalletRepository.save(bhpcWallet);
                                }
                            } else {
                                //当选择增加币种该操作,更新钱包余额
                                bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().add(batchPlayCoin.getCoinSum()));
                                bhpcWallet.setBalance(bhpcWallet.getBalance().add(batchPlayCoin.getCoinSum()));
                                batchPlayCoin.setCoinStatus("已打币");
                                batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                bhpcWalletRepository.save(bhpcWallet);
                            }
                        }
                    } else if (batchPlayCoin.getCoinType().equalsIgnoreCase("btc")) {
                        BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(user.getUserId());
                        if (null == bitCoinWallet) {
                            //选择减少操作,则提示用户余额为0;选择增加操作,给用户创建钱包,同时更新钱包的总额跟可用余额
                            if (batchPlayCoin.getOperation().equals("减少")) {
                                batchPlayCoin.setCoinStatus("数量不足");
                            } else {
                                bitCoinWallet = (BitCoinWallet) myWallet.createBitWallet(user.getUserId(), user.getEmail()).getObj();
                                bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().add(batchPlayCoin.getCoinSum()));
                                bitCoinWallet.setBalance(bitCoinWallet.getBalance().add(batchPlayCoin.getCoinSum()));
                                batchPlayCoin.setCoinStatus("已打币");
                                batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                bitcoinWalletRepository.save(bitCoinWallet);
                            }
                        } else {
                            //当选择减少币种该操作,判定用户的钱包数量是否足够
                            if (batchPlayCoin.getOperation().equals("减少")) {
                                //数量不足,返回提示;数量足够更新钱包余额
                                if (bitCoinWallet.getAvailBalance().compareTo(batchPlayCoin.getCoinSum()) == -1) {
                                    batchPlayCoin.setCoinStatus("数量不足");
                                } else {
                                    bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().subtract(batchPlayCoin.getCoinSum()));
                                    bitCoinWallet.setBalance(bitCoinWallet.getBalance().subtract(batchPlayCoin.getCoinSum()));
                                    batchPlayCoin.setCoinStatus("已打币");
                                    batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                    bitcoinWalletRepository.save(bitCoinWallet);
                                }
                            } else {
                                //当选择增加币种该操作,更新钱包余额
                                bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().add(batchPlayCoin.getCoinSum()));
                                bitCoinWallet.setBalance(bitCoinWallet.getBalance().add(batchPlayCoin.getCoinSum()));
                                batchPlayCoin.setCoinStatus("已打币");
                                batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                bitcoinWalletRepository.save(bitCoinWallet);
                            }
                        }
                    } else if (batchPlayCoin.getCoinType().equalsIgnoreCase("eth")) {
                        EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(user.getUserId());
                        if (null == ethereumWallet) {
                            //选择减少操作,则提示用户余额为0;选择增加操作,给用户创建钱包,同时更新钱包的总额跟可用余额
                            if (batchPlayCoin.getOperation().equals("减少")) {
                                batchPlayCoin.setCoinStatus("数量不足");
                            } else {
                                ethereumWallet = (EthereumWallet) myWallet.createEthWallet(user.getUserId()).getObj();
                                ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().add(batchPlayCoin.getCoinSum()));
                                ethereumWallet.setBalance(ethereumWallet.getBalance().add(batchPlayCoin.getCoinSum()));
                                batchPlayCoin.setCoinStatus("已打币");
                                batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                ethereumWalletRepository.save(ethereumWallet);
                            }
                        } else {
                            //当选择减少币种该操作,判定用户的钱包数量是否足够
                            if (batchPlayCoin.getOperation().equals("减少")) {
                                //数量不足,返回提示;数量足够更新钱包余额
                                if (ethereumWallet.getAvailBalance().compareTo(batchPlayCoin.getCoinSum()) == -1) {
                                    batchPlayCoin.setCoinStatus("数量不足");
                                } else {
                                    ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().subtract(batchPlayCoin.getCoinSum()));
                                    ethereumWallet.setBalance(ethereumWallet.getBalance().subtract(batchPlayCoin.getCoinSum()));
                                    batchPlayCoin.setCoinStatus("已打币");
                                    batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                    ethereumWalletRepository.save(ethereumWallet);
                                }
                            } else {
                                //当选择增加币种该操作,更新钱包余额
                                ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().add(batchPlayCoin.getCoinSum()));
                                ethereumWallet.setBalance(ethereumWallet.getBalance().add(batchPlayCoin.getCoinSum()));
                                batchPlayCoin.setCoinStatus("已打币");
                                batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                ethereumWalletRepository.save(ethereumWallet);
                            }
                        }
                    }else if (batchPlayCoin.getCoinType().equalsIgnoreCase("bhpcBouns")) {
                        Reward reward = rewardRepository.findByUserId(user.getUserId());
                        if (null == reward) {
                            //选择减少操作,则提示用户余额为0;选择增加操作,给用户创建钱包,同时更新钱包的总额跟可用余额
                            if (batchPlayCoin.getOperation().equals("减少")) {
                                batchPlayCoin.setCoinStatus("数量不足");
                            } else {
                                reward = (Reward) myWallet.createReward(user.getUserId()).getObj();
                                reward.setAvailBalance(reward.getAvailBalance().add(batchPlayCoin.getCoinSum()));
                                reward.setBalance(reward.getBalance().add(batchPlayCoin.getCoinSum()));
                                batchPlayCoin.setCoinStatus("已打币");
                                batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                rewardRepository.save(reward);
                            }
                        } else {
                            //当选择减少币种该操作,判定用户的钱包数量是否足够
                            if (batchPlayCoin.getOperation().equals("减少")) {
                                //数量不足,返回提示;数量足够更新钱包余额
                                if (reward.getAvailBalance().compareTo(batchPlayCoin.getCoinSum()) == -1) {
                                    batchPlayCoin.setCoinStatus("数量不足");
                                } else {
                                    reward.setAvailBalance(reward.getAvailBalance().subtract(batchPlayCoin.getCoinSum()));
                                    reward.setBalance(reward.getBalance().subtract(batchPlayCoin.getCoinSum()));
                                    batchPlayCoin.setCoinStatus("已打币");
                                    batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                    rewardRepository.save(reward);
                                }
                            } else {
                                //当选择增加币种该操作,更新钱包余额
                                reward.setAvailBalance(reward.getAvailBalance().add(batchPlayCoin.getCoinSum()));
                                reward.setBalance(reward.getBalance().add(batchPlayCoin.getCoinSum()));
                                batchPlayCoin.setCoinStatus("已打币");
                                batchPlayCoinService.saveBatchPlayRecord(batchPlayCoin, user);
                                rewardRepository.save(reward);
                            }
                        }
                    }
                }
            }
        }
        //本次批量打币的数据保存到数据库
        BatchPlayCoinRecord batchPlayCoinRecord = new BatchPlayCoinRecord();
        batchPlayCoinRecord.setImportData(JSONArray.fromObject(BatchPlayCoinService.batchPlayCoinList).toString());
        batchPlayCoinRecord.setOperationName(currentStaff().getRealname());
        batchPlayCoinRecord.setImportTime(new Date());
        batchPlayCoinRecordRepository.save(batchPlayCoinRecord);
        return new Resp("打币成功");
    }

    //打币导入功能
    @PostMapping("file")
    @ResponseBody
    public Resp file(MultipartFile file) {

        Resp resp = null;
        try {
            resp = batchPlayCoinService.excelImport(file);
        } catch (Exception e) {
            e.printStackTrace();
            return new Resp(Resp.PARAM_ERROR, e.getMessage());
        }
        return resp;
    }
}
